import Vue from 'vue'
import Vuex from 'vuex'
import loginApi from '@/api/login.js'
import common from '@/api/common.js'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex); //vue的插件机制

// Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: {
		state() {
			return {
				userId:'',
				userDetail:{},
				userList:{}
			}
		},

	},
	mutations: {
		MLOGIN(state, userId) {
			state.userId = userId
		},
		SETUSER(state,userDetail){
			state.userDetail = userDetail
		},
		MLOGOUT(state) {
			uni.clearStorage()
			state.userName = '已退出登录'
		},
		SET_USERLIST(state,userList){
			state.userList = userList
		}
	},
	actions: {
		login(context, userId) {
			context.commit('MLOGIN', userId)
			loginApi.getInfo({user_id:userId}).then(res=>{
				context.commit('SETUSER', res.data.detail)
			})
			common.getUserList().then((res) => {
			    this.userList = res.data.list.data;
			  context.commit('SET_USERLIST', res.data.list.data)
			
			});
		},
		userList(context, userList){
			context.commit('SET_USERLIST', userList)
		},
		logout(context) {
			context.commit('MLOGOUT')
		}
	},
	// plugins 插件配置
	plugins: [
		createPersistedState({
			paths: ['token', 'userId','userDetail','userList'],
			storage: { // 存储方式定义  
				getItem: (key) => uni.getStorageSync(key), // 获取  
				setItem: (key, value) => uni.setStorageSync(key, value), // 存储  
				removeItem: (key) => uni.removeStorageSync(key) // 删除  
			}
		})
	]
})
export default store